package com.hm.ibsm.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.TypeReference;
import com.hm.common.network.httpclient.HttpClientFactory;
import com.hm.common.network.httpclient.HttpClientResponseParse;
import com.hm.common.su.ServerResponseParse;
import com.hm.common.su.bean.ServerResponse;
import com.hm.ibsm.manage.enums.DictTypeEnums;
import com.hm.ibsm.manage.vo.dict.DictTypeValueVo;
import com.hm.ibsm.util.LocationConfig;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月7日 下午1:26:31
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
public class DictService {

	@Autowired
	private LocationConfig locationConfig;

	protected static String GET_DICT_TYPE_VALUES = "/dict/{0}";

	protected static String GET_DICT_VALUES = "/dict/values/{0}";

	protected static String GET_DICT_VALUE = "/dict/value/{0}";

	private static String MODEL_NAME = AuthService.class.getName();

	public List<DictTypeValueVo> getDictTypeValues(DictTypeEnums dictType) {
		try {
			HttpResponse response = locationConfig.accredit(HttpClientFactory.GET.build(locationConfig.mgUri(GET_DICT_TYPE_VALUES), dictType)).execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<List<DictTypeValueVo>>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return new ArrayList<DictTypeValueVo>();
	}

	public DictTypeValueVo getDictValue(String code) {
		try {
			HttpResponse response = locationConfig.accredit(HttpClientFactory.GET.build(locationConfig.mgUri(GET_DICT_VALUE), code)).execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<DictTypeValueVo>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return null;
	}

	public List<DictTypeValueVo> getDictValues(String code) {
		try {
			HttpResponse response = locationConfig.accredit(HttpClientFactory.GET.build(locationConfig.mgUri(GET_DICT_VALUES), code)).execute();
			return ServerResponseParse.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<List<DictTypeValueVo>>>() {
			}));
		} catch (Exception e) {
			log.error(MODEL_NAME, e);
		}
		return new ArrayList<DictTypeValueVo>();
	}
}
